package com.company.project.email.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.project.email.entity.EmailEntity;
import com.company.project.email.mapper.EmailMapper;
import com.company.project.email.service.EmailService;
import com.company.project.sys.entity.SysContentEntity;
import com.company.project.sys.mapper.SysContentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;

/**
 * 发送邮件接口
 * @author ZYF
 * @date 2021/8/24 16:11
 */
@Service
@Slf4j
public class EmailServiceImpl extends ServiceImpl<EmailMapper, EmailEntity> implements EmailService {

    //注入发送邮件的Impl模板
    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @NacosValue(value = "${spring.mail.username:2102378781@qq.com}", autoRefreshed = true)
    private String from ;

    @NacosValue(value = "${spring.mail.nickName:ZYF}", autoRefreshed = true)
    private String nickName;

    /**
     * 发送邮件(普通文件)
     *
     * @param emailEntity
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    @Override
    public void sendMail(EmailEntity emailEntity) throws MessagingException, UnsupportedEncodingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //设置邮件主题
        messageHelper.setSubject(emailEntity.getSubject());
        //这是邮件内容
        messageHelper.setText(emailEntity.getText());
        //设置发送给谁
//        message.setTo("jenney@momeike.com");
        messageHelper.setTo(emailEntity.getReceive());
        //设置发送人是谁（自己的邮箱名称）
        messageHelper.setFrom(from,nickName);
       //发送邮箱
        javaMailSender.send(mimeMessage);

    }

    /**
     * 发送带附件的邮件
     * @param emailEntity
     * @throws MessagingException
     */
    @Override
    public void sendAttachmentMail(EmailEntity emailEntity) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(from);
        messageHelper.setTo(emailEntity.getReceive());
        messageHelper.setSubject(emailEntity.getSubject());
        messageHelper.setText(emailEntity.getText(), true);
        FileSystemResource file=new FileSystemResource(new File(emailEntity.getFilePath()));
        String fileName=emailEntity.getFilePath().substring(emailEntity.getFilePath().lastIndexOf(File.separator));
        messageHelper.addAttachment(fileName,file);
        javaMailSender.send(mimeMessage);
    }

    /**
     * 发送html文件
     * @param emailEntity
     * @throws MessagingException
     */
    @Override
    public void sendSimpleMailHtml(EmailEntity emailEntity) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(from);
        helper.setTo(emailEntity.getReceive());
        helper.setSubject(emailEntity.getSubject());
        // 注意<img/>标签，src='cid:jpg'，'cid'是contentId的缩写，'jpg'是一个标记
        helper.setText("<html><body><img src=\"cid:jpg\"></body></html>", true);
        // 加载文件资源，作为附件
        FileSystemResource file=new FileSystemResource(new File(emailEntity.getFilePath()));
        // 调用MimeMessageHelper的addInline方法替代成文件('jpg[标记]', file[文件])
        helper.addInline("jpg", file);
        // 发送邮件
        javaMailSender.send(mimeMessage);
    }
}
